package com.superzlc.vjoy;

public class TestInt2Bytes {
	public static void main(String[] args) {
		new TestInt2Bytes().test();

	}

	void test()  {
		axis1 = 32767;
		axis2 = 32767;
		axis3 = 32767;
		axis4 = 32767;

		axis1 = 0;
		axis2 = 32767 >> 1;
		axis3 = 32767 >> 2;
		axis4 = 32769;

		System.out.println(this.toString());
		byte[] bytes = getBytes();
		this.parse(bytes, bytes.length);
		System.out.println(this.toString());
	}

	int axis1, axis2, axis3, axis4;

	public String toString() {
		return String.format("VJoyPackage: %d %d %d %d", axis1, axis2, axis3, axis4);
	}

	public byte[] getBytes() {
		byte[] bytes = new byte[16];
		int idx = 0;
		int v = axis1;
		bytes[idx++] = (byte)((v >>> 24) & 0xFF);
		bytes[idx++] = (byte)((v >>> 16) & 0xFF);
		bytes[idx++] = (byte)((v >>> 8) & 0xFF);
		bytes[idx++] = (byte)((v >>> 0) & 0xFF);

		v = axis2;
		bytes[idx++] = (byte)((v >>> 24) & 0xFF);
		bytes[idx++] = (byte)((v >>> 16) & 0xFF);
		bytes[idx++] = (byte)((v >>> 8) & 0xFF);
		bytes[idx++] = (byte)((v >>> 0) & 0xFF);

		v = axis3;
		bytes[idx++] = (byte)((v >>> 24) & 0xFF);
		bytes[idx++] = (byte)((v >>> 16) & 0xFF);
		bytes[idx++] = (byte)((v >>> 8) & 0xFF);
		bytes[idx++] = (byte)((v >>> 0) & 0xFF);

		v = axis4;
		bytes[idx++] = (byte)((v >>> 24) & 0xFF);
		bytes[idx++] = (byte)((v >>> 16) & 0xFF);
		bytes[idx++] = (byte)((v >>> 8) & 0xFF);
		bytes[idx++] = (byte)((v >>> 0) & 0xFF);
		return bytes;
	}

	public void parse(byte[] data, int size) {
		int idx = 0;
		// axis1 = ((data[idx++] << 24) + (data[idx++] << 16) + (data[idx++] << 8) + (data[idx++] << 0));
//		axis2 = ((data[idx++] << 24) + (data[idx++] << 16) + (data[idx++] << 8) + (data[idx++] << 0));
//		axis3 = ((data[idx++] << 24) + (data[idx++] << 16) + (data[idx++] << 8) + (data[idx++] << 0));
//		axis4 = ((data[idx++] << 24) + (data[idx++] << 16) + (data[idx++] << 8) + (data[idx++] << 0));
		axis1 = (int) ( ((data[idx++] & 0xFF)<<24)
	            |((data[idx++] & 0xFF)<<16)
	            |((data[idx++] & 0xFF)<<8)
	            |(data[idx++] & 0xFF));
		axis2 = (int) ( ((data[idx++] & 0xFF)<<24)
	            |((data[idx++] & 0xFF)<<16)
	            |((data[idx++] & 0xFF)<<8)
	            |(data[idx++] & 0xFF));
		axis3 = (int) ( ((data[idx++] & 0xFF)<<24)
	            |((data[idx++] & 0xFF)<<16)
	            |((data[idx++] & 0xFF)<<8)
	            |(data[idx++] & 0xFF));
		axis4 = (int) ( ((data[idx++] & 0xFF)<<24)
	            |((data[idx++] & 0xFF)<<16)
	            |((data[idx++] & 0xFF)<<8)
	            |(data[idx++] & 0xFF));
	}

}
